#!coding=utf-8

from numpy import *
import time

def loadDataset(filename):
    dataMat=[]
    fr=open(filename)
    for line in fr.readlines():
        lineArr=line.strip().split()读取每一行文件，切分数据
        l=len(lineArr)
        if l<7:
#            lineArr.append(0)
#            lineArr.append(0)
			dataMat.append([int(lineArr[0]),float(lineArr[1]),float(lineArr[2]),0,0,0,0])存到矩阵
			continue
        dataMat.append([int(lineArr[0]),float(lineArr[1]),float(lineArr[2]),lineArr[3],lineArr[4],float(lineArr[5]),float(lineArr[6])])
    return dataMat

def sessionGet(mat):
    sessionMat=[]
    for i in range(len(mat)):
        if mat[i][3]==mat[0][3] and mat[i][5]==mat[0][5]:
            if mat[i][4]==mat[0][4] and mat[i][6]==mat[0][6]:
                if mat[i][1]-mat[0][1]<15:		#设置判断流的时间间隔
                    sessionMat.append(i+1)
#				sessionMat.append(i+1)
        elif mat[i][3]==mat[0][4] and mat[i][5]==mat[0][6]:
            if mat[i][4] == mat[0][3] and mat[i][6]==mat[0][5]:
                if mat[i][1]-mat[0][1]<15:		#设置判断流的时间间隔
                    sessionMat.append(i+1)
#				sessionMat.append(i+1)
    return sessionMat

def sessionList(filename):
    sessionLis=[]
    a=loadDataset(filename)
    while len(a)>0:
        k=[]
        b=sessionGet(a)
        for i in range(len(b)):
            k.extend([a[b[i]-1][0]])
        sessionLis.append(k)
        for i in range(len(b)):
            del a[b[i]-1-i]
    return sessionLis

def featureGet(a,vec):
    pnum=0;nnum=0;plong=0;nlong=0;pmax=0;nmax=0;sumtime=0
#    a=loadDataset(filename)
    k=[]
    if len(vec)>10:
		l=10
    else:
		l=len(vec)
    for i in range(l):
		if i<l-1:
			sumtime+=a[vec[i+1]-1][1]-a[vec[i]-1][1]
#			maxtime=a[vec[i+1]-1][1]-a[vec[i]-1][1]
		k.extend([a[vec[i]-1][2]])    
		if a[vec[i]-1][5]==a[vec[0]-1][5]:
			pnum=pnum+1
			plong=plong+a[vec[i]-1][2]
			if a[vec[i]-1][2]>pmax:
				pmax=a[vec[i]-1][2]
		elif a[vec[i]-1][5]==a[vec[0]-1][6]:
			nnum=nnum+1
			nlong=nlong+a[vec[i]-1][2]
			if a[vec[i]-1][2]>nmax:
				nmax=a[vec[i]-1][2]
    if plong==0:
        pave=0
    else:
        pave=float(plong)/pnum
    if nlong==0:
        nave=0
    else:
        nave=float(nlong)/nnum
    if l==1:
		avetime=0
    else:
		avetime=float(sumtime)/(l-1)
    return [pnum,nnum,pave,nave,pmax,nmax,avetime,var(k)]

def featureList(filename):
    featureLis=[]
    a=loadDataset(filename)
    b=sessionList(filename)
    for j in range(len(b)):
        k=featureGet(a,b[j])
        featureLis.append(k)
    return featureLis

def loadLabels(filename):
    labelMat=[]
    fr=open(filename)
    for line in fr.readlines():
        lineArr=line.strip().split(':')
        labelMat.append(lineArr)
    return labelMat

def labelsList(filename1,filename2):
    i=0
    labelList=[]
    flag=1
    fr=open(filename1)
    a=loadDataset(filename2)
    mat=sessionList(filename2)
    x=featureList(filename2)
    for line in fr.readlines():
	timeStart=time.time()
        lineArr=line.strip().split(':')
        flag=1
        while flag>0:
            if a[mat[i][0]-1][1]==float(lineArr[0]):
                flag=0
                i=i+1
                if lineArr[5]=='ssl;':
                    labelList.append(1)
                else:
                    labelList.append(0)
            else:
                del x[len(labelList)]
                i=i+1
            if i==len(mat):
                break
        if i==len(mat):
            break
#	timeEnd=time.time()
#	print (timeEnd-timeStart)*len(x)/60
    return x,labelList
            
